﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Partial Class FrontEnd_Control_Store_MenuLeft
    Inherits System.Web.UI.UserControl
    Public curStoreURL As String = ""
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.RouteData.Values("Id") Is Nothing Then
            LoadMenu(Page.RouteData.Values("Id"))

        End If
    End Sub
    Protected Sub LoadMenu(ByVal StoreId As Integer)
        Using data As New VSEntities
            Dim p = (From q In data.Stores Where q.StoreId = StoreId Select q).FirstOrDefault
            If Not p Is Nothing Then
                curStoreURL = MainDomain & p.QueryString & "-" & p.StoreId & "/"
                Dim CateStoreId = (From q In data.StoreTypes Where q.StoreID = StoreId Select q.RootCateId).FirstOrDefault

                Dim k = (From q In data.Categories Where q.ParentId = CateStoreId).ToList

                With rptMenu
                    .DataSource = k
                    .DataBind()

                End With

            End If
        End Using
    End Sub

    Protected Sub rptMenu_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptMenu.ItemDataBound
        Dim row As VSModel.Category = CType(e.Item.DataItem, VSModel.Category)

        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.NavigateUrl = curStoreURL & row.QueryString & "-" & row.CatId.ToString & "/danh-sach.html"
        hplTitle.Text = row.Title.ToString

        'bind child menu
        Dim item As RepeaterItem = e.Item
        If item.ItemType = ListItemType.Item Or item.ItemType = ListItemType.AlternatingItem Then
            Dim rptChild = CType(item.FindControl("rptMenuChild"), Repeater)
            Using data As New VSEntities
                Dim fn = (From q In data.Categories Where q.ParentId = row.CatId).ToList
                With rptChild
                    .DataSource = fn
                    .DataBind()
                End With
            End Using
        End If
        'end bind menu
    End Sub
    Protected Sub rptMenuChild_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)

        Dim row As VSModel.Category = CType(e.Item.DataItem, VSModel.Category)
        Dim hplTitle As HyperLink = CType(e.Item.FindControl("hplTitle"), HyperLink)
        hplTitle.NavigateUrl = curStoreURL & row.QueryString & "-" & row.CatId.ToString & "/danh-sach.html"
        hplTitle.Text = row.Title.ToString

    End Sub

   
End Class
